package com.frost.aop.proxy;

import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

// 创建代理对象并使用
public class DynamicMain {
    public static void main(String[] args) {
        // JDK动态代理
//        HouseSubject target = new RealHouseSubject(); // 目标类
//        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(
//                target.getClass().getClassLoader(),
//                new Class[]{HouseSubject.class},
//                new JDKInvocationHandler(target));
//        proxy.rentHouse();
//        proxy.saleHouse();

        // 使用JDK动态代理测试普通类, 会报错
//        RealHouseSubject2 target = new RealHouseSubject2(); // 目标类
//        RealHouseSubject2 proxy = (RealHouseSubject2) Proxy.newProxyInstance(
//                target.getClass().getClassLoader(),
//                new Class[]{RealHouseSubject2.class},
//                new JDKInvocationHandler(target));
//        proxy.rentHouse();
//        proxy.saleHouse();

        // CGLib动态代理
//        HouseSubject target = new RealHouseSubject(); // 目标类
//        HouseSubject proxy = (HouseSubject) Enhancer.create(target.getClass(), new CGLibMethodInterceptor(target));
//        proxy.rentHouse();
//        proxy.saleHouse();

        // 使用CGLib动态代理测试普通类
        RealHouseSubject2 target = new RealHouseSubject2(); // 目标类
        RealHouseSubject2 proxy = (RealHouseSubject2) Enhancer.create(target.getClass(), new CGLibMethodInterceptor(target));
        proxy.rentHouse();
        proxy.saleHouse();
    }
}
